import User from "../pages/User.vue";
import Entrust from "../pages/Entrust.vue";
import Info from "../pages/Info.vue";
import Address from "../pages/Address.vue";
import Login from "../pages/Login.vue";
import Register from "../pages/Register.vue";
import EditUser from "../pages/EditUser.vue";
import EditEntrust from "../pages/EditEntrust.vue";
import * as VueRouter from "vue-router";
import {currentUser, whitePath} from "./globalConfig";
import {Toast} from "vant";

const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes: [
        {
            path: '/user',
            component: User
        },
        {
            path: '/entrust',
            component: Entrust
        },
        {
            path: '/info',
            component: Info
        },
        {
            path: '/address',
            component: Address
        },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/register',
            component: Register
        },
        {
            path: '/editUser',
            component: EditUser
        },
        {
            path: '/editEntrust',
            component: EditEntrust
        },
    ],

})

router.beforeEach((to, from) => {
    // 不是白名单的 并且没有登录的情况下 重定向到登录页面
    if (currentUser.id === 0 &&
        whitePath.indexOf(to.fullPath) === -1 &&
        to.name !== 'login') {
        Toast('此功能还需要登录哦')
        return {path: '/login'}
    }
})

export default router